﻿using System;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using ArtheaEngine;

namespace ArtheaServer
{
    public partial class ServerForm : Form
    {
        public ServerForm()
        {
            InitializeComponent();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (startToolStripMenuItem.Text == "&Start")
            {
                Server.Instance.Start();
                startToolStripMenuItem.Text = "&Stop";
            }
            else
            {
                Server.Instance.Stop();
                startToolStripMenuItem.Text = "&Start";
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Server.Instance.Stop();
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var buf = new StringBuilder();

            AssemblyName assName = Assembly.GetAssembly(typeof (World)).GetName(false);

            Version vrs = assName.Version;

            buf.Append(assName.Name).Append(": v");
            buf.AppendFormat("{0}.{1}.{2}", vrs.Major, vrs.Minor, vrs.Build);
            buf.AppendLine();
            buf.AppendLine();

            assName = Assembly.GetAssembly(typeof (Server)).GetName(false);

            vrs = assName.Version;

            buf.Append(assName.Name).Append(": v");
            buf.AppendFormat("{0}.{1}.{2}", vrs.Major, vrs.Minor, vrs.Build);
            buf.AppendLine();
            buf.AppendLine();

            buf.AppendLine("Copyright " + DateTime.Now.ToString("yyyy") + " Ryan Jennings Inc.");
            buf.AppendLine();
            buf.AppendLine("All rights reserved. See http://arthea.googlecode.com/ for more information.");

            MessageBox.Show(buf.ToString(), "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void notifier_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            RestoreMainForm();
        }

        private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Server.Instance.Stop();
        }

        private void ServerForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState && minimizeToTrayToolStripMenuItem.Checked)
            {
                Hide();
                notifier.Visible = true;
            }
        }

        private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RestoreMainForm();
        }

        private void RestoreMainForm()
        {
            Show();
            notifier.Visible = false;
            WindowState = FormWindowState.Normal;
        }

        private void txtOutput_Enter(object sender, EventArgs e)
        {
            mainMenu.Focus();
        }
    }
}